package org.jeecg.modules.wmhb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.modules.wmhb.entity.WmFlowDept;
import org.jeecg.modules.wmhb.entity.WmItem;
import org.jeecg.modules.wmhb.entity.WmTask;
import org.jeecg.modules.wmhb.entity.WmTaskFlow;
import org.jeecg.modules.wmhb.mapper.WmTaskFlowMapper;
import org.jeecg.modules.wmhb.service.IWmTaskFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 任务流程时间
 * @Author: jeecg-boot
 * @Date:   2024-09-13
 * @Version: V1.0
 */
@Service
public class WmTaskFlowServiceImpl extends ServiceImpl<WmTaskFlowMapper, WmTaskFlow> implements IWmTaskFlowService {
    @Autowired
    ISysBaseAPI sysBaseApi;

    @Override
    public void saveTaskFlow(List<WmFlowDept> wmFlowDeptList, WmTask wmTask){
        List<WmTaskFlow> wmTaskFlowList = queryByTaskId(wmTask.getId());
        for(WmFlowDept wmFlowDept: wmFlowDeptList){
            WmTaskFlow wmTaskFlow = new WmTaskFlow();
            wmTaskFlow.setDepartId(wmFlowDept.getDeptId());
            wmTaskFlow.setResponsibility(wmFlowDept.getResponsibility());
            wmTaskFlow.setWmTaskId(wmTask.getId());
            wmTaskFlow.setSysOrgCode(wmFlowDept.getSysOrgCode());
            wmTaskFlow.setOrderNum(wmFlowDept.getOrderNum());
            if(!CollectionUtils.isEmpty(wmTaskFlowList)) {
                for(WmTaskFlow wmTaskFlowItem: wmTaskFlowList){
                    if(wmTaskFlowItem.getDepartId().equals(wmFlowDept.getDeptId()) && wmTaskFlowItem.getOrderNum().equals(wmFlowDept.getOrderNum())) {
                        wmTaskFlow.setResponsibility(wmTaskFlowItem.getResponsibility());
                        wmTaskFlow.setChargeBy(wmTaskFlowItem.getChargeBy());
                        wmTaskFlow.setFinishTime(wmTaskFlowItem.getFinishTime());
                        break;
                    }
                }
            }
            save(wmTaskFlow);
        }
    };

    @Override
    public WmTaskFlow getTaskFlowByTaskAndOrder(String wmTaskId, Integer orderNum){
        LambdaQueryWrapper<WmTaskFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskFlow::getWmTaskId, wmTaskId);
        queryWrapper.eq(WmTaskFlow::getOrderNum, orderNum).last("LIMIT 1");;
        WmTaskFlow wmTaskFlow = getOne(queryWrapper);
        return wmTaskFlow;
    };

    @Override
    public void updateFactBeginTimeByStep(List<WmTask> wmTaskList, Integer orderNum) {
        List<String> idList = new ArrayList<>();
        for(WmTask wmTask: wmTaskList){
            idList.add(wmTask.getId());
        }

        LambdaUpdateWrapper<WmTaskFlow> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmTaskFlow::getFactBeginTime, new Date()).in(WmTaskFlow::getWmTaskId, idList).eq(WmTaskFlow::getOrderNum, orderNum);
        update(null, updateWrapper);
    }

    @Override
    public void updateFactEndTimeByStep(List<WmTask> wmTaskList, Integer orderNum) {
        List<String> idList = new ArrayList<>();
        for(WmTask wmTask: wmTaskList){
            idList.add(wmTask.getId());
        }

        LambdaUpdateWrapper<WmTaskFlow> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WmTaskFlow::getFactEndTime, new Date()).in(WmTaskFlow::getWmTaskId, idList).eq(WmTaskFlow::getOrderNum, orderNum);
        update(null, updateWrapper);
    }

    @Override
    public WmTaskFlow getByTaskAndDepart(String wmTaskId, String departId, Integer orderNum){
        LambdaQueryWrapper<WmTaskFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskFlow::getWmTaskId, wmTaskId);
        queryWrapper.eq(WmTaskFlow::getDepartId, departId);
        queryWrapper.eq(WmTaskFlow::getOrderNum, orderNum).last("LIMIT 1");;
        WmTaskFlow wmTaskFlow = getOne(queryWrapper);
        return wmTaskFlow;
    };

    @Override
    public List<WmTaskFlow> queryByTaskId(String wmTaskId) {
        LambdaQueryWrapper<WmTaskFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskFlow::getWmTaskId, wmTaskId).orderByAsc(WmTaskFlow::getOrderNum);
        List<WmTaskFlow> wmTaskFlowList = new ArrayList<>();
        List<WmTaskFlow> wmTaskFlowListQuery = list(queryWrapper);
        for(WmTaskFlow wmTaskFlow: wmTaskFlowListQuery){
            SysDepartModel sysDepartModel = sysBaseApi.selectAllById(wmTaskFlow.getDepartId());
            wmTaskFlow.setDepartName(sysDepartModel.getDepartName());
            wmTaskFlowList.add(wmTaskFlow);
        }
        return wmTaskFlowList;
    }

    @Override
    public WmTaskFlow getByTaskAndIndex(String wmTaskId, Integer orderNum){
        LambdaQueryWrapper<WmTaskFlow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmTaskFlow::getWmTaskId, wmTaskId);
        queryWrapper.eq(WmTaskFlow::getOrderNum, orderNum).last("LIMIT 1");;
        WmTaskFlow wmTaskFlow = getOne(queryWrapper);
        return wmTaskFlow;
    };
}
